﻿using System.Globalization;
using System.Windows.Controls;

namespace FireUI.Windows.ValidationRules
{
    public class LengthValidationRule : ValidationRule
    {
        public int? MinLength { get; set; }
        public int? MaxLength { get; set; }

        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            if (value is not string text)
            {
                return new ValidationResult(false, "value must be a string");
            }

            if (MinLength is not null && text.Length < MinLength)
            {
                return new ValidationResult(false, $"Value must be at least {MinLength} charactors long");
            }

            if (MaxLength is not null && text.Length > MaxLength)
            {
                return new ValidationResult(false, $"Value must be at most {MaxLength} charactors long");
            }
            return ValidationResult.ValidResult;
        }
    }
}